<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/9/4
 * Time: 11:34
 */
require('../../../common.inc.php');

define('SITE_AUTH', 'activity-line-banner');

use lib\core\AdminAction;
use Mobile\IndexFeedBanner as RD;
use Act\LinkConfig;

class banner extends AdminAction
{
    private $list_url = 'data/modules/activity/banner.php';

    public function doDefault()
    {
        $smarty = $this->app->page();
        //活动链接获取
        $info = LinkConfig::getList(['Id >'=>0])['data'];
        $link = [];
        foreach($info as $v){
            $link[$v['LinkUrl']] = $v['Name'];
        }

        $contData = RD::getData(RD::APP_ACTIVE_BANNER);
        $contData = $contData[RD::APP_ACTIVE_BANNER];
        if (!empty($contData)) {
            $smarty->value('link',$link);
            $smarty->value('contlist', $contData);
        }
        $smarty->output('activity/banner_list.tpl');
    }

    public function doAdd()
    {
        $smarty = $this->app->page();
        $id = intval(trim($_GET['t']));

        //活动链接获取
        $info = LinkConfig::getList(['Id >'=>0])['data'];
        $link = [];
        foreach($info as $v){
            $link[$v['LinkUrl']] = $v['Name'];
        }

        if ($id < 0) {
            HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
        }
        if (!empty($id)) {
            $contData = RD::getData(RD::APP_ACTIVE_BANNER);
            $contData = $contData[RD::APP_ACTIVE_BANNER];
            if (empty($contData[$id])) {
                HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
            }
            $smarty->value('info', $contData[$id]);
        }
        $smarty->value('link',$link);
        $smarty->output('activity/banner_edit.tpl');
    }

    public function doUpdata()
    {
        $link = trim($_POST['link']);
        $start_time = (isset($_POST['start_time']) and $_POST['start_time'] !== '') ? $_POST['start_time'] : null;
        $end_time = (isset($_POST['end_time']) and $_POST['end_time'] !== '') ? $_POST['end_time'] : null;
        $start_version = (isset($_POST['start_version']) and trim($_POST['start_version']) !== '') ? $_POST['start_version'] : null;
        $end_version = (isset($_POST['end_version']) and trim($_POST['end_version']) !== '') ? $_POST['end_version'] : null;
        $order = (isset($_POST['order']) and trim($_POST['order']) !== '') ? $_POST['order'] : 0;


        //修改数据入库
        if (!empty($_POST['k'])) {
            $contData = RD::getData(RD::APP_ACTIVE_BANNER)[RD::APP_ACTIVE_BANNER];
            $contData_k = RD::getData(RD::APP_ACTIVE_BANNER)[RD::APP_ACTIVE_BANNER][$_POST['k']];
            if (isset($_FILES['img']['name']) && $_FILES['img']['error'] == 0 && $_FILES['img']['size'] > 0) {
                $imgUrlAddre = RD::saveImageUrl($_FILES['img']['tmp_name']);
            } else {
                $imgUrlAddre = $contData_k[RD::FIELD_CONTENT_IMAGE_URL];
            }
                $new = $contData_k[RD::FIELD_CONTENT_K];
            $newdata = [
                $new => [
                    RD::FIELD_CONTENT_K => $new,
                    RD::FIELD_CONTENT_URL => $link,
                    RD::FIELD_CONTENT_IMAGE_URL => $imgUrlAddre,
                    RD::FIELD_CONTENT_START_TIME => $start_time,
                    RD::FIELD_CONTENT_END_TIME => $end_time,
                    RD::FIELD_CONTENT_MIN_VERSION => $start_version,
                    RD::FIELD_CONTENT_MAX_VERSION => $end_version,
                    RD::FIELD_CONTENT_ORDER => $order,
                ]
            ];

            $data =$newdata + $contData;

        }else{
            //数据入库
            if (isset($_FILES['img']['name']) && $_FILES['img']['error'] == 0 && $_FILES['img']['size'] > 0) {
                // 复制图片
                $imgUrlAddre = RD::saveImageUrl($_FILES['img']['tmp_name']);
                $new = time();

                $contData = RD::getData(RD::APP_ACTIVE_BANNER);
                $contData = $contData[RD::APP_ACTIVE_BANNER];

                if (!empty($contData)) {
                    $newdata = [
                        $new => [
                            RD::FIELD_CONTENT_K => $new,
                            RD::FIELD_CONTENT_URL => $link,
                            RD::FIELD_CONTENT_IMAGE_URL => $imgUrlAddre,
                            RD::FIELD_CONTENT_START_TIME => $start_time,
                            RD::FIELD_CONTENT_END_TIME => $end_time,
                            RD::FIELD_CONTENT_MIN_VERSION => $start_version,
                            RD::FIELD_CONTENT_MAX_VERSION => $end_version,
                            RD::FIELD_CONTENT_ORDER => $order,
                        ]
                    ];
                    $data = $contData + $newdata;

                } else {
                    $data = [
                        $new => [
                            RD::FIELD_CONTENT_K => $new,
                            RD::FIELD_CONTENT_URL => $link,
                            RD::FIELD_CONTENT_IMAGE_URL  => $imgUrlAddre,
                            RD::FIELD_CONTENT_START_TIME => $start_time,
                            RD::FIELD_CONTENT_END_TIME => $end_time,
                            RD::FIELD_CONTENT_MIN_VERSION => $start_version,
                            RD::FIELD_CONTENT_MAX_VERSION  => $end_version,
                            RD::FIELD_CONTENT_ORDER => $order,
                        ]
                    ];
                }
            }
        }

        $data = RD::getOrder($data, RD::FIELD_CONTENT_ORDER);
        $m_data = [];
        foreach ($data as $v) {
            $m_data[$v['K']] = $v;
        }
        $data = $m_data;

        $preservation = RD::setData(RD::APP_ACTIVE_BANNER, $data);
        if ($preservation) {
            HDshowMsg('保存成功', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('操作失败', ['type' => 'error']);
        }

    }

    public function doDelete()
    {
        $time = $_GET['t'];
        $contData = RD::getData(RD::APP_ACTIVE_BANNER);
        if (!empty($contData[RD::APP_ACTIVE_BANNER])) {
            $contData = $contData[RD::APP_ACTIVE_BANNER];
        }
        unset($contData[$time]);
        $preservation = RD::setData(RD::APP_ACTIVE_BANNER, $contData);
        if ($preservation) {
            HDshowMsg('删除成功', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('操作失败', ['type' => 'error']);
        }
    }


}

$app->run();